package flow.model;

import java.awt.Color;
import java.awt.Container;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JTextArea;


import flow.conf.Configuration;
import flow.conf.Configuration.FlowType;
import flow.conf.GraphConstant;
import flow.conf.VertexType;

public class Vertex extends Element {
	public JTextArea textArea;
	private Point startP;
	private int fontsize;
	private Point endP;
	private Color borderColor;
	private VertexType vertexType;
	public Color  getBorderColor(){
		if(selected){
			return GraphConstant.DEFAULT_SELECTED_COLOR;
		}
		return borderColor;
	}
	public Color  getColor(){
		return color;
	}
	public Vertex(Color color,Point sp,Point ep,int id) {
		
		super(color,2);
		this.startP=sp;
		this.endP=ep;
		this.id=id;
		
		borderColor=GraphConstant.DEFAULT_ELEMENT_BORDERCOLOR;
		/*shape =new Rectangle2D.Double(
				p1.getX(),
				p1.getY(),
				p2.getX()-p1.getX(),
				p2.getY()-p1.getY()
				);*/
		
	}
	public Vertex(Color color, Point point, int id,VertexType vertextype,JComponent component) {
		
		super(color,2);
		this.startP=point;
		this.vertexType=vertextype;
		this.endP=new Point(point.x+GraphConstant.DEFAULT_WIDTH,point.y+GraphConstant.DEFAULT_HEIGHT);
		this.id=id;
		initTextArea( component);
		borderColor=GraphConstant.DEFAULT_ELEMENT_BORDERCOLOR;
		/*shape =new Rectangle2D.Double(
				p1.getX(),
				p1.getY(),
				p2.getX()-p1.getX(),
				p2.getY()-p1.getY()
				);*/
	}
	
	public void  initTextArea(JComponent component){
		textArea=new JTextArea();
		textArea.setLineWrap(true);
		textArea.setAutoscrolls(true);
		textArea.setBorder(BorderFactory.createEmptyBorder());
		textArea.setEditable(true);
		
		component.add(textArea);
		
		showTextArea();
	}
	public void moveTo(int x,int y){
		startP.x=x;
		endP.x=x;
		startP.y=y;
		endP.y=y;
	}
	
	public void moveBy(int x,int y){
		startP.x+=x;
		endP.x+=x;
		startP.y+=y;
		endP.y+=y;
	}
	
	public void hideTextArea(){
		textArea.hide();
		name =textArea.getText();
	}
	public void showTextArea(){
		textArea.setBounds(getStartP().x+2,getStartP().y+2,
				getEndP().x-getStartP().x-4,
				getEndP().y-getStartP().y-4);
		textArea.show();
	}
	public Shape getShape() {
		// TODO Auto-generated method stub
		RoundRectangle2D.Double juxing =new RoundRectangle2D.Double(
				startP.getX(),
				startP.getY(),
				endP.getX()-startP.getX(),
				endP.getY()-startP.getY(),40,40
				);
		return juxing;
	}
	
	public Point getStartP() {
		return startP;
	}
	public void setStartP(Point startP) {
		this.startP = startP;
	}
	public Point getEndP() {
		return endP;
	}
	public void setEndP(Point endP) {
		this.endP = endP;
	}
	public VertexType getVertexType() {
		return vertexType;
	}
	public void setVertexType(VertexType vertexType) {
		this.vertexType = vertexType;
	}
	public void setBorderColor(Color borderColor) {
		this.borderColor = borderColor;
	}
	public void setEditable(boolean bool){
		textArea.setEditable(bool);
	}
	@Override
	public boolean near(int x, int y) {
		return getShape().getBounds().contains(x,y);
	}
}
